﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.ServiceModel;

namespace DMSWcfServices
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Xml que retorna los eventos de la pagina
            Response.Expires = 0;
            Response.ExpiresAbsolute = DateTime.MinValue;
            Response.AddHeader("PRAGMA", "NO-CACHE");
            Response.AddHeader("Expires", "-1");
            Response.CacheControl = "no-cache";
            Response.ContentType = "text/xml";



            WSHttpBinding binding = new WSHttpBinding();
            binding.CloseTimeout = new TimeSpan(0, 1, 0);
            binding.OpenTimeout = new TimeSpan(0, 1, 0);
            binding.ReceiveTimeout = new TimeSpan(0, 10, 0);
            binding.SendTimeout = new TimeSpan(0, 2, 0);
            binding.Security.Mode = SecurityMode.None;

            EndpointAddress address = new EndpointAddress(Request.QueryString["PSDS"].ToString());

            DMSServices.DMSServicesClient service = new DMSServices.DMSServicesClient(binding, address);

            List<Dtl.OrderInvoice> listInvoice = null;
            try 
            {
                //+ "&PSDS=" + Properties.Settings.Default.Server
                listInvoice = service.getOInvoiceReport(Request.QueryString["idDistributor"].ToString(), Convert.ToDateTime(Request.QueryString["dtInvoiceStart"].ToString()), Convert.ToDateTime(Request.QueryString["dtInvoiceEnd"].ToString()), Request.QueryString["ambient"].ToString());
            }
            catch( Exception ee)
            {
                listInvoice= new List<Dtl.OrderInvoice>();
            }
            

            XmlTextWriter cdRSS = new XmlTextWriter(this.Response.OutputStream, System.Text.Encoding.UTF8);
            cdRSS.WriteStartDocument();
            cdRSS.WriteStartElement("OrderInvoice");

            if(listInvoice.Count>0)
            {                
                    //cdRSS.WriteStartElement("mes");
                    //cdRSS.WriteAttributeString("numero", "prueba");                
                    foreach (Dtl.OrderInvoice oi in listInvoice)
                    {
                        cdRSS.WriteStartElement("order");
                        cdRSS.WriteAttributeString("cdOrder", oi.cdOrder);                        
                        cdRSS.WriteAttributeString("cdCreationUser", oi.cdCreationUser);
                        cdRSS.WriteAttributeString("cdStore", oi.cdStore);
                        cdRSS.WriteAttributeString("cdRegion", oi.cdRegion.ToString());
                        cdRSS.WriteAttributeString("dtOrder", oi.dtOrder.ToShortDateString());
                        cdRSS.WriteAttributeString("vlDiscountTotal", oi.vlDiscountTotal.ToString());
                        cdRSS.WriteAttributeString("vlTotalOrder", oi.vlTotalOrder.ToString());
                        cdRSS.WriteAttributeString("vlTotalUnit",oi.vlTotalUnit.ToString());
                        cdRSS.WriteAttributeString("cdRoute", oi.cdRoute);
                        cdRSS.WriteAttributeString("cdStatusOrder", oi.cdStatusOrder);
                        cdRSS.WriteAttributeString("cdInvoice", oi.cdInvoice);
                        cdRSS.WriteAttributeString("dsDisplayText", oi.dsDisplayText);
                        cdRSS.WriteAttributeString("dtInvoice", oi.dtInvoice.ToShortDateString());
                        cdRSS.WriteAttributeString("vlTotalInvoice", oi.vlTotalInvoice.ToString());
                        cdRSS.WriteAttributeString("nrTotalQuantity", oi.nrTotalQuantity.ToString());
                        cdRSS.WriteAttributeString("cdStatusInvoice", oi.cdStatusInvoice.ToString());                        

                        cdRSS.WriteEndElement();
                    }
                    //cdRSS.WriteEndElement();
                
               }
            //cdRSS.WriteEndElement();
            cdRSS.WriteEndDocument();
            cdRSS.Flush();
            cdRSS.Close();
            this.Response.End();

            }                      
        }
    }
